﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TOOLS;
using Model;
using DAL;
namespace BLL
{
    public partial class LogicAPI:BaseBll
    {

        Cached cached = new Cached();
        public Dictionary<string, string> Register(Dictionary<string, string> p)
        {
            bool status=UserCenter.Register(p["username"],p["password"],int.Parse(p["script_id"]));
            Dictionary<string, string> result = new Dictionary<string, string>();
            
            if (status)
            {
                result.Add("status","");
                result.Add("message", "注册成功！");
            }
            else
            {
                result.Add("status", "用户名已经存在！");
                result.Add("message", "用户名已经存在！");
            }
            //result.Add("proc", p["proc"]);
            return result;
        }
        public Dictionary<string, string> Login(Dictionary<string, string> p)
        {
            string loginKey ;
            Model.tb_user currentUser = UserCenter.Login(p["username"], p["password"], int.Parse(p["script_id"]),out loginKey);
            Dictionary<string, string> result = new Dictionary<string, string>();
            bool flag = loginKey != "";
            if (flag)
            {
                var nowVersion= DataSetting.GetKeyValue("version_"+p["script_id"]);
                if (nowVersion.value.Split('|')[0] == p["version"])
                {
                    result.Add("status", "");
                    result.Add("user_name", currentUser.user_name);
                    result.Add("server_time", DateTime.Now.ToString());
                    result.Add("end_time", currentUser.expiration_time.ToString());
                    result.Add("login_key", loginKey);
                    result.Add("message", "登录成功");}
                else
                {
                    result.Add("status", nowVersion.value.Split('|')[1]);
                    result.Add("message", nowVersion.value.Split('|')[1]);
                }
            }
            else
            {
                result.Add("status", "用户名或密码错误");
                result.Add("message","用户名或密码错误");
            }
            //result.Add("proc", p["proc"]);
            return result;
        }
        public Dictionary<string, string> Logout(Dictionary<string, string> p)
        {
      
            bool flag = UserCenter.Logout(p["login_key"]);
            Dictionary<string, string> result = new Dictionary<string, string>();
        
            if (flag)
            {
                result.Add("status", "");
                result.Add("message", "注销成功");
            }
            else
            {
                result.Add("status", "注销失败");
                result.Add("message", "注销失败");
            }
            //result.Add("proc", p["proc"]);
            return result;
        }
        public Dictionary<string, string> Recharge(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            string temp = UserCenter.Recharge(p["cdkey"], p["login_key"]);
            result.Add("status", temp);
            if (temp=="")
            {
                result.Add("message", "成功");
            }
            else
            {
                result.Add("message", temp);
            }
            
            //result.Add("proc", p["proc"]);
            return result;
        }
        public Dictionary<string, string> ResetPassword(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
             string temp = UserCenter.ResetPassword(p["old_password"],p["new_password"],p["login_key"]);
            result.Add("status", temp);
            if (temp=="")
            {
                 result.Add("message", "修改成功"); 
            }
            else
            {
                result.Add("message", temp);
            }
            //result.Add("proc", p["proc"]);
            return result;
        }
        public Dictionary<string, string> Heartbeat(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            Model.tb_user user1;
            string temp = UserCenter.Heartbeat(p["login_key"],out user1);
            if (temp=="")
            {
                result.Add("server_time", DateTime.Now.ToString());
                result.Add("end_time", user1.expiration_time.ToString());
                result.Add("end_time2", user1.super_expiration_time.ToString());
            }
            result.Add("status", temp);
            result.Add("message", temp);
            //result.Add("proc", p["proc"]);
            return result;
        }



       
        public Dictionary<string, string> MHeartbeat(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            Model.tb_m_user user1;
            string temp = MUserCenter.Heartbeat(p["login_key"], out user1);
            result.Add("status", temp);
            result.Add("message", "");
            //result.Add("proc", p["proc"]);
            return result;
        }
        public Dictionary<string, string> GetAllKey(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            string temp = DataSetting.GetAllKey(p["login_key"]);
            if (temp!=null)
            {
                result.Add("status","");
                result.Add("message", temp);
            }
            else
            {
                result.Add("status", "无数据");
                result.Add("message", "无数据");
            }
            //result.Add("proc", p["proc"]);
            return result;
        }
        public Dictionary<string, string> MLogin(Dictionary<string, string> p)
        {
            string loginKey;
            Model.tb_m_user currentUser = MUserCenter.Login(p["username"], p["password"], out loginKey);
            Dictionary<string, string> result = new Dictionary<string, string>();
            bool flag = loginKey != "";
            if (flag)
            {
                result.Add("status", "");
                result.Add("login_key", loginKey);
                result.Add("message", "登录成功");
            }
            else
            {
                result.Add("status", "用户名或密码错误");
                result.Add("message", "用户名或密码错误");
            }
            //result.Add("proc", p["proc"]);
            return result;
        }
        public Dictionary<string, string> GetKeyValue(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = MHeartbeat(p);
            if (result["status"]=="")
            {
                var keyValueEntity = DataSetting.GetKeyValue(p["key"]);
                if (keyValueEntity==null)
                {
                    result["status"] = "该键值数据不存在";
                    result["message"] = "该键值数据不存在";
                }
                else
                {
                    result.Add("key", keyValueEntity.key_id);
                    result.Add("value", keyValueEntity.value);
                    result.Add("note", keyValueEntity.note);
                }
               
            }
            return result;
           
        }
        public Dictionary<string, string> CGetKeyValue(Dictionary<string, string> p)
        {
            Model.tb_user user1;
            string temp = UserCenter.Heartbeat(p["login_key"], out user1);
            Dictionary<string, string> result = Heartbeat(p);
            if (result["status"] == "")
            {
                var keyValueEntity = DataSetting.GetKeyValue(p["key"]);
                if (keyValueEntity == null)
                {
                    result["status"] = "该键值数据不存在";
                    result["message"] = "该键值数据不存在";
                }
                else
                {
                    
                    result.Add("key", keyValueEntity.key_id);
                    result.Add("value", keyValueEntity.value.DESEncrypt((user1.user_name + DateTime.Now.ToString("MM-dd-HH") + p["login_key"]).GetMD5().Substring(0, 8)));
                    result.Add("note", keyValueEntity.note);
                }
            }
            return result;
        }
        public Dictionary<string, string> AddKeyValue(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = MHeartbeat(p);
            if (result["status"] == "")
            {
                 bool flag=  DataSetting.AddKeyValue(p["key"]);
                if (flag)
                {
                    result["message"] = "添加成功";
              
                }
                else
                {
                    result["status"] = "添加失败";
                    result["message"] = "添加失败";
                }

            }
            return result;

        }

        public Dictionary<string, string> UpdateKeyValue(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = MHeartbeat(p);
            if (result["status"] == "")
            {
                bool flag = DataSetting.UpdateKeyValue(p["key"],p["value"],p["note"]);
                if (flag)
                {
                    result["message"] = "修改成功";

                }
                else
                {
                    result["status"] = "修改失败";
                    result["message"] = "修改失败";
                }

            }
            return result;

        }

        public Dictionary<string, string> GetFA(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = Heartbeat(p);
            if (result["status"] == "")
            {
                Dictionary<string,string> temp=  DataSetting.GetFA();
                if (temp!=null)
                {
                    foreach (var item in temp)
                    {
                        result.Add(item.Key, item.Value);
                    }
                }
                else
                {
                    result["status"] = "获取数据失败";
                    result["message"] = "获取数据失败";
                }

            }
            return result;

        }
        public Dictionary<string, string> GetCDKeys(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = MHeartbeat(p);
            if (result["status"] == "")
            {
                   List<Model.tb_cdkey> data = CDKey.GetCDKeys(p);
                    result["message"] = data.ToJSON();
            }
            return result;
        }
        public Dictionary<string, string> CreateCDKey(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = MHeartbeat(p);
            if (result["status"] == "")
            {
                bool flag = CDKey.CreateCDKey(p);
                if (flag)
                {
                    result["status"] = "";
                    result["message"] = "生成成功";
                }
                else
                {
                    result["status"] = "生成失败";
                    result["message"] = "生成失败";
                }
               
            }
            return result;
        }
        public Dictionary<string, string> GetUserList(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = MHeartbeat(p);
            if (result["status"] == "")
            {
               List<Model.tb_user> ulist=  UserCenter.GetUserList(p);
               result["message"] = ulist.ToJSON();
            }
            return result;
        }
        public Dictionary<string, string> ChangeToGeneralTime(Dictionary<string, string> p)
        {
            Dictionary<string, string> result =Heartbeat(p);
            if (result["status"] == "")
            {
                string msg;
                bool status = UserCenter.ChangeToGeneralTime(BaseBll.loginUserList[p["login_key"]].loginUser.uid, p["hour"].ToInt(), out msg);
                result["status"] = status ? "" : msg;
                result["message"] = msg;
                if (status)
                {
                    BaseBll.loginUserList[p["login_key"]].loginUser = BaseBll.loginUserList[p["login_key"]].loginUser.ReLoad();
                }
            }
            return result;
        }
        public  Dictionary<string, string> ChangeToSuperTime(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = Heartbeat(p);
            if (result["status"] == "")
            {
                string msg;
                bool status = UserCenter.ChangeToSuperTime(BaseBll.loginUserList[p["login_key"]].loginUser.uid, p["hour"].ToInt(), out msg);
                result["status"] = status ? "" : msg;
                result["message"] = msg;
                if (status)
                {
                    BaseBll.loginUserList[p["login_key"]].loginUser = BaseBll.loginUserList[p["login_key"]].loginUser.ReLoad();
                }
            }
            return result;
        }
        public Dictionary<string, string> GetChangeTimeConfig(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = Heartbeat(p);
            if (result["status"] == "")
            {
                Model.tb_key_value  temp= DataSetting.GetChangeTimeConfig(BaseBll.loginUserList[p["login_key"]].loginUser.script_id);
                result["message"] = temp.value;
            }
            return result;
        }
        public Dictionary<string, string> ReadData(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = Heartbeat(p);
            if (result["status"] == "")
            {

                result["message"] = cached.ReadData(p["data_section"],p["data_key"]);
            }
            return result;
        }
        public Dictionary<string, string> WriteData(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = Heartbeat(p);
            if (result["status"] == "")
            {
                result["message"] = cached.WriteData(p["data_section"], p["data_key"],p["data_val"]);
            }
            return result;
        }
        public Dictionary<string, string> OnKeyCheck(Dictionary<string, string> p)
        { 
              Dictionary<string, string> result = new Dictionary<string, string>();
            tb_user temp=  UserCenter.ExistUser(p["username"], p["password"], int.Parse(p["script_id"]));
           
            if (temp==null)
            {
                result.Add("status", "用户名或密码错误");
                result.Add("message", "用户名或密码错误");
            }
            else
            {
                List<LoginInfo> loginlist = BaseBll.loginUserList.Values.ToList();
                if (null == loginlist.Where(a => a.loginStatus && a.loginUser.user_name == p["username"] && a.loginUser.script_id == int.Parse(p["script_id"])).FirstOrDefault())
                {
                    result.Add("status", "帐号不在线");
                    result.Add("message", "帐号不在线");
                }
                else
                {
                    result.Add("status", "");
                }
            }
            //result.Add("proc", p["proc"]);
            return result;
           
        }
       
        public Dictionary<string, string> OneKeyReadData(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            result = OnKeyCheck(p);
            if (result["status"] == "")
            {
                result["message"] = cached.ReadData(p["data_section"], p["data_key"]);
            }
            return result;
        }
        public Dictionary<string, string> OneKeyWriteData(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            result = OnKeyCheck(p);
            if (result["status"] == "")
            {
                result["message"] = cached.WriteData(p["data_section"], p["data_key"], p["data_val"]);
            }
            return result;
        }
        public Dictionary<string, string> SaveString(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();

            result.Add("status", "");
            result.Add("message", "");
            Log log = new Log();
            tb_Log logentity = new tb_Log();
            logentity.createTime = DateTime.Now;
            logentity.value1 = p["value1"];
            logentity.value2 = p["value2"];
            logentity.valueContent = p["valueContent"];
            DAL.User user = new User();
            if (user.Count(a => a.user_name == logentity.value2) == 0 || logentity.value1 != "203.195.168.234")
            {
                log.Add(logentity);
                EnLog("发现盗版,不存在用户名或者IP地址不对：{0},服务器IP:{1}", logentity.value2, logentity.value1);
                result["status"] = "不存在用户名或者IP地址不对";
            }
            else if (!IsLogin(logentity.value2))
            {
                log.Add(logentity);
                EnLog("发现盗版，用户不在线：{0},服务器IP:{1}", logentity.value2, logentity.value1);
                result["status"] = "帐号不在线";
            }
            return result;
        }
    }
}

